﻿using System.Collections.Generic;
using FireStarterModeller.BO;
using FireStarterModeller.UI;
using FireStarterModeller.UI.LookupLists;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;
using Rhino.Mocks;

namespace FireStarterModeller.Test.UI.LookupLists
{
    [TestFixture]
    public class TestLookupTypeSelectorControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

//        private static IControlFactory GetControlFactory()
//        {
//            return GlobalUIRegistry.ControlFactory;
//        }

        private static IFireStarterFormsController CreateFireStarterFormsControllerStub(
            out ILookupListControl noneLookupListControl, out ILookupListControl simpleLookupListControl,
            out ILookupListControl businessObjectLookupListControl)
        {
            IFireStarterFormsController fireStarterFormsController =
                MockRepository.GenerateStub<IFireStarterFormsController>();
            fireStarterFormsController.Stub(t => t.ControlFactory).Return(new ControlFactoryWin());

            noneLookupListControl = CreateLookupListControlStub();
            fireStarterFormsController.Stub(t => t.CreateNoneLookupTypeControl()).Return(noneLookupListControl);

            simpleLookupListControl = CreateLookupListControlStub();
            fireStarterFormsController.Stub(t => t.CreateSimpleLookupTypeControl()).Return(simpleLookupListControl);

//            databaseLookupListControl = CreateLookupListControlStub();
//            fireStarterFormsController.Stub(t => t.CreateDatabaseLookupTypeControl()).Return(databaseLookupListControl);

            businessObjectLookupListControl = CreateLookupListControlStub();
            fireStarterFormsController.Stub(t => t.CreateBusinessObjectLookupTypeControl()).Return(
                businessObjectLookupListControl);

            return fireStarterFormsController;
        }

        private static ILookupListControl CreateLookupListControlStub()
        {
            LookupTypeControlStub lookupListControl = new LookupTypeControlStub();
            lookupListControl.LookupTypeName = TestUtilsShared.GetRandomString();
            return lookupListControl;
        }

        [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl; 
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof (UserControlWin), lookupTypeSelectorControl);
            Assert.AreSame(fireStarterFormsController, lookupTypeSelectorControl.FireStarterFormsController);
        }

        [Test]
        public void Test_Construct_ShouldCreateLookupTypeControls()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(noneLookupListControl);
            Assert.IsNotNull(simpleLookupListControl);
//            Assert.IsNotNull(databaseLookupListControl);
            Assert.IsNotNull(businessObjectLookupListControl);
            //---------------Execute Test ----------------------
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            //---------------Test Result -----------------------
            List<ILookupListControl> lookupTypeControls = lookupTypeSelectorControl.LookupTypeControls;
            Assert.Contains(noneLookupListControl, lookupTypeControls, "Expected to contain noneLookupListControl");
            Assert.Contains(simpleLookupListControl, lookupTypeControls, "Expected to contain simpleLookupListControl");
//            Assert.Contains(databaseLookupListControl, lookupTypeControls, "Expected to contain databaseLookupListControl");
            Assert.Contains(businessObjectLookupListControl, lookupTypeControls,
                            "Expected to contain businessObjectLookupListControl");
            Assert.AreEqual(3, lookupTypeControls.Count);
            fireStarterFormsController.AssertWasCalled(controller => controller.CreateNoneLookupTypeControl());
            fireStarterFormsController.AssertWasCalled(controller => controller.CreateSimpleLookupTypeControl());
//            fireStarterFormsController.AssertWasCalled(controller => controller.CreateDatabaseLookupTypeControl());
            fireStarterFormsController.AssertWasCalled(controller => controller.CreateBusinessObjectLookupTypeControl());
        }

        [Test]
        public void Test_DMProperty()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            lookupTypeSelectorControl.DMProperty = property;
            //---------------Test Result -----------------------
            Assert.AreSame(property, lookupTypeSelectorControl.DMProperty);
        }

        [Test]
        public void Test_DMProperty_WhenSet_ShouldSetDmPropertyOnLookupListTypeControls()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            lookupTypeSelectorControl.DMProperty = property;
            //---------------Test Result -----------------------
            Assert.AreSame(property, noneLookupListControl.Property);
            Assert.AreSame(property, simpleLookupListControl.Property);
//            Assert.AreSame(property, databaseLookupListControl.Property);
            Assert.AreSame(property, businessObjectLookupListControl.Property);
        }

        [Test]
        public void Test_DMProperty_WhenSetToNull_ShouldSetNullDmPropertyOnLookupListTypeControls()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty();
            lookupTypeSelectorControl.DMProperty = property;
            //---------------Assert Precondition----------------
            Assert.AreSame(property, noneLookupListControl.Property);
            Assert.AreSame(property, simpleLookupListControl.Property);
//            Assert.AreSame(property, databaseLookupListControl.Property);
            Assert.AreSame(property, businessObjectLookupListControl.Property);
            //---------------Execute Test ----------------------
            lookupTypeSelectorControl.DMProperty = null;
            //---------------Test Result -----------------------
            Assert.IsNull(noneLookupListControl.Property);
            Assert.IsNull(simpleLookupListControl.Property);
//            Assert.IsNull(databaseLookupListControl.Property);
            Assert.IsNull(businessObjectLookupListControl.Property);
        }

        // Test Layout - Combo Box, Panel
        [Test]
        public void Test_Layout()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            //---------------Test Result -----------------------
            Assert.AreEqual(2, lookupTypeSelectorControl.Controls.Count);
            IPanel topPanel = TestUtilsShared.AssertIsInstanceOf<IPanel>(lookupTypeSelectorControl.Controls[1]);
            ILabel label = TestUtilsShared.AssertIsInstanceOf<ILabel>(topPanel.Controls[0]);
            Assert.AreEqual("Select Lookup Type", label.Text);
            IComboBox comboBox = TestUtilsShared.AssertIsInstanceOf<IComboBox>(topPanel.Controls[1]);
            Assert.AreSame(comboBox, lookupTypeSelectorControl.LookupTypeComboBox);
            IPanel panel = TestUtilsShared.AssertIsInstanceOf<IPanel>(lookupTypeSelectorControl.Controls[0]);
            Assert.AreSame(panel, lookupTypeSelectorControl.LookupTypeControlPanel);
        }

        // Test Combo - Has List same as List of type controls(.Name Property)
        // - it must have a "None" item
        [Test]
        public void Test_Combo_ShouldContainLookupTypeNames()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(noneLookupListControl.LookupTypeName);
            Assert.IsNotNull(simpleLookupListControl.LookupTypeName);
//            Assert.IsNotNull(databaseLookupListControl.LookupTypeName);
            Assert.IsNotNull(businessObjectLookupListControl.LookupTypeName);
            //---------------Execute Test ----------------------
            //---------------Test Result -----------------------
            Assert.AreEqual(3, lookupTypeSelectorControl.LookupTypeControls.Count);
            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;
            Assert.IsNotNull(lookupTypeComboBox);
            Assert.IsTrue(lookupTypeComboBox.Items.Contains(noneLookupListControl.LookupTypeName),
                          "Should contain 'noneLookupListControl' item");
            Assert.IsTrue(lookupTypeComboBox.Items.Contains(simpleLookupListControl.LookupTypeName),
                          "Should contain 'simpleLookupListControl' item");
//            Assert.IsTrue(lookupTypeComboBox.Items.Contains(databaseLookupListControl.LookupTypeName), "Should contain 'databaseLookupListControl' item");
            Assert.IsTrue(lookupTypeComboBox.Items.Contains(businessObjectLookupListControl.LookupTypeName),
                          "Should contain 'businessObjectLookupListControl' item");
            Assert.AreEqual(3, lookupTypeComboBox.Items.Count);
        }

        // Test Select Item - Sets relevant control docked Fill in panel
        [Test]
        public void Test_ComboBox_WhenItemSelected_ShouldSetRelevantControlInPanel()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl; 
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;
            ILookupListControl lookupListControl = GetRandomLookupTypeControl(lookupTypeSelectorControl);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(lookupListControl);
            Assert.AreEqual(3, lookupTypeComboBox.Items.Count);
            Assert.AreEqual(0, lookupTypeSelectorControl.LookupTypeControlPanel.Controls.Count);
            Assert.IsNull(lookupTypeComboBox.SelectedItem);
            //---------------Execute Test ----------------------
            lookupTypeComboBox.SelectedItem = lookupListControl.LookupTypeName;
            //---------------Test Result -----------------------
            Assert.AreEqual(lookupListControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(1, lookupTypeSelectorControl.LookupTypeControlPanel.Controls.Count);
            Assert.AreEqual(lookupListControl, lookupTypeSelectorControl.LookupTypeControlPanel.Controls[0]);
            Assert.AreEqual(DockStyle.Fill, lookupListControl.Dock);
        }

        [Test]
        public void Test_ComboBox_WhenItemSelected_ThenNothingSelected_ShouldClearControlInPanel()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;
            ILookupListControl lookupListControl = SelectRandomLookupTypeControl(lookupTypeSelectorControl);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(lookupListControl);
            Assert.AreEqual(3, lookupTypeComboBox.Items.Count);
            Assert.AreEqual(lookupListControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(1, lookupTypeSelectorControl.LookupTypeControlPanel.Controls.Count);
            Assert.AreEqual(lookupListControl, lookupTypeSelectorControl.LookupTypeControlPanel.Controls[0]);
            Assert.AreEqual(DockStyle.Fill, lookupListControl.Dock);
            //---------------Execute Test ----------------------
            lookupTypeComboBox.SelectedItem = null;
            //---------------Test Result -----------------------
            Assert.IsNull(lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(0, lookupTypeSelectorControl.LookupTypeControlPanel.Controls.Count);
        }

        [Test]
        public void Test_ComboBox_WhenItemSelected_ThenAnotherItemSelected_ShouldChangeControlInPanel()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;
            ILookupListControl originalSelectedLookupListControl = lookupTypeSelectorControl.LookupTypeControls[0];

            lookupTypeComboBox.SelectedItem = originalSelectedLookupListControl.LookupTypeName;

            int selectedIndex = TestUtilsShared.GetRandomInt(1, 3);
            ILookupListControl lookupListControl = lookupTypeSelectorControl.LookupTypeControls[selectedIndex];
            //---------------Assert Precondition----------------
            Assert.IsNotNull(lookupListControl);
            Assert.AreEqual(3, lookupTypeComboBox.Items.Count);
            Assert.AreEqual(originalSelectedLookupListControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(1, lookupTypeSelectorControl.LookupTypeControlPanel.Controls.Count);
            Assert.AreEqual(originalSelectedLookupListControl,
                            lookupTypeSelectorControl.LookupTypeControlPanel.Controls[0]);
            Assert.AreEqual(DockStyle.Fill, originalSelectedLookupListControl.Dock);
            //---------------Execute Test ----------------------
            lookupTypeComboBox.SelectedItem = lookupListControl.LookupTypeName;
            //---------------Test Result -----------------------
            Assert.IsNotNull(lookupListControl);
            Assert.AreEqual(3, lookupTypeComboBox.Items.Count);
            Assert.AreEqual(lookupListControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(1, lookupTypeSelectorControl.LookupTypeControlPanel.Controls.Count);
            Assert.AreEqual(lookupListControl, lookupTypeSelectorControl.LookupTypeControlPanel.Controls[0]);
            Assert.AreEqual(DockStyle.Fill, lookupListControl.Dock);
        }

        // Test Select Item - Set DMProperty.LookupList to relevantPanel.LookupList
        [Test]
        public void Test_ComboBox_WhenItemSelected_ShouldSetRelevantControlsLookupListToTheDMProp()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController)
                    {
                        DMProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty()
                    };
            lookupTypeSelectorControl.DMProperty.LookupList = null;
            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;
            ILookupListControl lookupListControl = GetRandomLookupTypeControl(lookupTypeSelectorControl);
            DMLookupList lookupList = lookupListControl.LookupList;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(lookupListControl);
            Assert.IsNotNull(lookupTypeSelectorControl.DMProperty);
            Assert.IsNull(lookupTypeSelectorControl.DMProperty.LookupList);
            Assert.AreEqual(3, lookupTypeComboBox.Items.Count);
            Assert.IsNull(lookupTypeComboBox.SelectedItem);
            //---------------Execute Test ----------------------
            lookupTypeComboBox.SelectedItem = lookupListControl.LookupTypeName;
            //---------------Test Result -----------------------
            Assert.AreEqual(lookupListControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            Assert.AreSame(lookupList, lookupTypeSelectorControl.DMProperty.LookupList);
        }

        [Test]
        public void Test_ComboBox_WhenItemSelected_ThenNothingSelected_ShouldSetDMPropLookupListToNull()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController)
                    {
                        DMProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty()
                    };
            lookupTypeSelectorControl.DMProperty.LookupList = null;
            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;

            ILookupListControl lookupListControl = SelectRandomLookupTypeControl(lookupTypeSelectorControl);
            DMLookupList lookupList = lookupListControl.LookupList;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(lookupListControl);
            Assert.IsNotNull(lookupTypeSelectorControl.DMProperty);
            Assert.AreEqual(3, lookupTypeComboBox.Items.Count);
            Assert.AreEqual(lookupListControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            Assert.AreSame(lookupList, lookupTypeSelectorControl.DMProperty.LookupList);
            //---------------Execute Test ----------------------
            lookupTypeComboBox.SelectedItem = null;
            //---------------Test Result -----------------------
            Assert.IsNull(lookupTypeComboBox.SelectedItem);
            Assert.IsNull(lookupTypeSelectorControl.DMProperty.LookupList);
        }

        private static ILookupListControl SelectRandomLookupTypeControl(
            LookupTypeSelectorControl lookupTypeSelectorControl)
        {
            ILookupListControl lookupListControl = GetRandomLookupTypeControl(lookupTypeSelectorControl);
            lookupTypeSelectorControl.LookupTypeComboBox.SelectedItem = lookupListControl.LookupTypeName;
            return lookupListControl;
        }

        private static ILookupListControl GetRandomLookupTypeControl(LookupTypeSelectorControl lookupTypeSelectorControl)
        {
            int selectedIndex = TestUtilsShared.GetRandomInt(0, 3);
            LookupTypeControlStub lookupTypeControl =
                (LookupTypeControlStub) lookupTypeSelectorControl.LookupTypeControls[selectedIndex];
            lookupTypeControl.LookupList = TestUtilsDMLookupList.CreateUnsavedValidDMLookupList();
            return lookupTypeControl;
        }

        [Test]
        public void
            Test_ComboBox_WhenItemSelected_ThenAnotherItemSelected_ShouldSetRelevantControlsLookupListToTheDMProp()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController)
                    {
                        DMProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty()
                    };
            lookupTypeSelectorControl.DMProperty.LookupList = null;
            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;

            LookupTypeControlStub originalSelectedLookupTypeControl =
                (LookupTypeControlStub) lookupTypeSelectorControl.LookupTypeControls[0];
            DMLookupList originalLookupList = TestUtilsDMLookupList.CreateUnsavedValidDMLookupList();
            originalSelectedLookupTypeControl.LookupList = originalLookupList;

            lookupTypeComboBox.SelectedItem = originalSelectedLookupTypeControl.LookupTypeName;

            int selectedIndex = TestUtilsShared.GetRandomInt(1, 3);
            LookupTypeControlStub lookupTypeControl =
                (LookupTypeControlStub) lookupTypeSelectorControl.LookupTypeControls[selectedIndex];
            DMLookupList lookupList = TestUtilsDMLookupList.CreateUnsavedValidDMLookupList();
            lookupTypeControl.LookupList = lookupList;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(lookupTypeControl);
            Assert.IsNotNull(lookupTypeSelectorControl.DMProperty);
            Assert.AreEqual(3, lookupTypeComboBox.Items.Count);
            Assert.AreEqual(originalSelectedLookupTypeControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            Assert.AreSame(originalLookupList, lookupTypeSelectorControl.DMProperty.LookupList);
            //---------------Execute Test ----------------------
            lookupTypeComboBox.SelectedItem = lookupTypeControl.LookupTypeName;
            //---------------Test Result -----------------------
            Assert.IsNotNull(lookupTypeControl);
            Assert.AreEqual(3, lookupTypeComboBox.Items.Count);
            Assert.AreEqual(lookupTypeControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            Assert.AreSame(lookupList, lookupTypeSelectorControl.DMProperty.LookupList);
        }

        // When DMProp is set to null, should select nothing in the combo, have an empty panel, and be disabled.
        [Test]
        public void Test_DMProperty_WhenSetToNull_ShouldClearAndDisableControl()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController)
                    {
                        DMProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty()
                    };
            lookupTypeSelectorControl.DMProperty.LookupList = null;
            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;
            IPanel lookupTypeControlPanel = lookupTypeSelectorControl.LookupTypeControlPanel;
            ILookupListControl lookupListControl = SelectRandomLookupTypeControl(lookupTypeSelectorControl);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(lookupTypeSelectorControl.DMProperty);
            Assert.IsNotNull(lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(1, lookupTypeControlPanel.Controls.Count);
            Assert.IsTrue(lookupTypeSelectorControl.Enabled);
            Assert.IsTrue(lookupTypeComboBox.Enabled);
            Assert.IsTrue(lookupTypeControlPanel.Enabled);
            //---------------Execute Test ----------------------
            lookupTypeSelectorControl.DMProperty = null;
            //---------------Test Result -----------------------
            Assert.IsNull(lookupTypeSelectorControl.DMProperty);
            Assert.IsNull(lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(0, lookupTypeControlPanel.Controls.Count);
            Assert.IsFalse(lookupTypeSelectorControl.Enabled, "Control should not be enabled");
            Assert.IsFalse(lookupTypeComboBox.Enabled, "Combo should not be enabled");
            Assert.IsFalse(lookupTypeControlPanel.Enabled, "Panel should not be enabled");
        }

        // When DMProp is set to something, should select the correct item in the list and enable controls
        // - i.e. when the lookuplist is null, the selected item should be the "None" item
        [Test]
        public void Test_DMProperty_WhenSetToSomething_ShouldEnableControlsAndSelectCorrectItem()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty();
            ILookupListControl lookupListControl = GetRandomLookupTypeControl(lookupTypeSelectorControl);
            dmProperty.LookupList = lookupListControl.LookupList;
            lookupTypeSelectorControl.DMProperty = null;
            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;
            IPanel lookupTypeControlPanel = lookupTypeSelectorControl.LookupTypeControlPanel;
            //---------------Assert Precondition----------------
            Assert.IsNull(lookupTypeSelectorControl.DMProperty);
            Assert.IsNull(lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(0, lookupTypeControlPanel.Controls.Count);
            Assert.IsFalse(lookupTypeSelectorControl.Enabled, "Control should not be enabled");
            Assert.IsFalse(lookupTypeComboBox.Enabled, "Combo should not be enabled");
            Assert.IsFalse(lookupTypeControlPanel.Enabled, "Panel should not be enabled");
            //---------------Execute Test ----------------------
            lookupTypeSelectorControl.DMProperty = dmProperty;
            //---------------Test Result -----------------------
            Assert.AreSame(dmProperty, lookupTypeSelectorControl.DMProperty);
            Assert.AreEqual(lookupListControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(1, lookupTypeControlPanel.Controls.Count);
            Assert.IsTrue(lookupTypeSelectorControl.Enabled, "Control should be enabled");
            Assert.IsTrue(lookupTypeComboBox.Enabled, "Combo should be enabled");
            Assert.IsTrue(lookupTypeControlPanel.Enabled, "Panel should be enabled");
        }

        [Test]
        public void Test_DMProperty_WhenSetToSomethingElse_ShouldSelectCorrectItem()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);

            DMProperty originalDMProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty();
            ILookupListControl originalLookupListControl = GetRandomLookupTypeControl(lookupTypeSelectorControl);
            originalDMProperty.LookupList = originalLookupListControl.LookupList;

            lookupTypeSelectorControl.DMProperty = originalDMProperty;

            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty();
            ILookupListControl lookupListControl = GetRandomLookupTypeControl(lookupTypeSelectorControl);
            dmProperty.LookupList = lookupListControl.LookupList;

            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;
            //---------------Assert Precondition----------------
            Assert.AreSame(originalDMProperty, lookupTypeSelectorControl.DMProperty);
            Assert.AreEqual(originalLookupListControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            //---------------Execute Test ----------------------
            lookupTypeSelectorControl.DMProperty = dmProperty;
            //---------------Test Result -----------------------
            Assert.AreSame(dmProperty, lookupTypeSelectorControl.DMProperty);
            Assert.AreEqual(lookupListControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
        }

        [Test]
        public void Test_DMProperty_WhenSetToSomethingElse_WithNonFoundLookup_ShouldSelectNoItem()
        {
            //---------------Set up test pack-------------------
            ILookupListControl noneLookupListControl;
            ILookupListControl simpleLookupListControl;
//            ILookupListControl databaseLookupListControl;
            ILookupListControl businessObjectLookupListControl;
            IFireStarterFormsController fireStarterFormsController =
                CreateFireStarterFormsControllerStub(out noneLookupListControl, out simpleLookupListControl,
                                                     out businessObjectLookupListControl);
            LookupTypeSelectorControl lookupTypeSelectorControl =
                new LookupTypeSelectorControl(fireStarterFormsController);

            DMProperty originalDMProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty();
            ILookupListControl lookupListControl = GetRandomLookupTypeControl(lookupTypeSelectorControl);
            originalDMProperty.LookupList = lookupListControl.LookupList;

            lookupTypeSelectorControl.DMProperty = originalDMProperty;

            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty();
            dmProperty.LookupList = TestUtilsDMLookupList.CreateUnsavedValidDMLookupList();
            IComboBox lookupTypeComboBox = lookupTypeSelectorControl.LookupTypeComboBox;
            IPanel lookupTypeControlPanel = lookupTypeSelectorControl.LookupTypeControlPanel;
            //---------------Assert Precondition----------------
            Assert.AreSame(originalDMProperty, lookupTypeSelectorControl.DMProperty);
            Assert.AreEqual(lookupListControl.LookupTypeName, lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(1, lookupTypeControlPanel.Controls.Count);
            //---------------Execute Test ----------------------
            lookupTypeSelectorControl.DMProperty = dmProperty;
            //---------------Test Result -----------------------
            Assert.AreSame(dmProperty, lookupTypeSelectorControl.DMProperty);
            Assert.IsNull(lookupTypeComboBox.SelectedItem);
            Assert.AreEqual(0, lookupTypeControlPanel.Controls.Count);
        }

        // Requirements for a ILookupListControl:
        // - When the DM Prop is set:
        //    - if the DmProp.LookupList is of the same type, then the control should return this LUL for the LookupList Property
        //    - if the DmProp.LookupList is of a different type, then the control should return a new LUL for the LookupList Property
        // - Should return correct LookupTypeName i.e. "None", "Simple"....
        // - Should display UIPanel with the correct fields

        // Names: NoneLookupTypeControl, SimpleLookupTypeControl, DatabaseLookupTypeControl, BusinessObjectLookupTypeControl
        // Specifically:
        // For The NoneLookupTypeControl:
        //  - The DMProperty will just act as a prop
        //  - The LookupList will always return null
        //  - The control will only contain a label saying like: "Please select a lookup list type....." look at old FS

        // Also to do: Write Tests for the FireStarterFormsController.
        //   - Should return these new controls from the Create methods

        private class LookupTypeControlStub : UserControlWin, ILookupListControl
        {
            public DMProperty Property { get; set; }
            public DMLookupList LookupList { get; set; }
            public string LookupTypeName { get; set; }
        }
    }
}